﻿using CodeGen.TS.Generation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGen.TS.Input
{
    public static class ParameterUsage
    {
        public static void UseParameters(string fileName, ParameterParser parser)
        {
            string generate = parser.ParseArgumentValue("Generate");
            if (generate == null)
            {
                if (parser.ContainsArgument("GenerateClass"))//For backwardcompatibility
                    generate = "class";
                else
                    generate = "interface";
            }

            bool useObservable = parser.ContainsArgument("UseObservable");
            bool useInitialValue = parser.ContainsArgument("UseInitalValue");
            string outputFileName = parser.ParseArgumentValue("Output");


            string generatedWebServiceMethod = null;
            if (parser.ContainsArgument("GeneratedWebServiceMethod"))
                generatedWebServiceMethod = parser.ParseArgumentValue("GeneratedWebServiceMethod");

            var assembly = AssemblyLoader.GetAssembly(fileName);
            var types = assembly.GetTypes();

            string stypesToGet = parser.ParseArgumentValue("types");
            if (stypesToGet == null)
            {
                Console.WriteLine("Write types to generate code from. You can separate them by using commas:");
                stypesToGet = Console.ReadLine();
            }
            List<string> typesToGet = stypesToGet.Split(',').Select(s => s.Trim()).ToList();
            string outputText = "";
            foreach (var type in types)
            {
                string typeName = type.FullName;
                if (typeName.Contains("."))
                {
                    typeName = type.FullName.Substring(type.FullName.LastIndexOf(".") + 1);
                }
                if (typesToGet.Contains(typeName))
                {

                    CodeGen.TS.Generation.ICodeGenerator generator = null;
                    if (generate == "class")
                    {
                        generator = new CodeGen.TS.Generation.Generators.ClassGenerator()
                                                {
                                                    GeneratedWebServiceMethod = generatedWebServiceMethod,
                                                    UseInitialValue = useInitialValue
                                                };
                    }
                    else if (generate == "interface")
                    {
                        generator = new Generation.Generators.InterfaceGenerator();

                    }
                    else if (generate == "apicontroller")
                    {
                        generator = new Generation.Generators.MVC.ApiController();
                    }

                    generator.Type = type;
                    generator.UseObservable = useObservable;

                    string output = generator.GenerateOutput();
                    outputText += Environment.NewLine + output + Environment.NewLine;

                    Console.WriteLine(output);
                    Console.WriteLine();
                    typesToGet.Remove(typeName);
                }
            }
            foreach (string tp in typesToGet)
            {
                Logger.LogWarning("Type {0} not found", tp);
            }
            if (!string.IsNullOrEmpty(outputFileName))
                Output.OutputWriter.WriteCode(outputFileName, outputText, useObservable);

        }
    }
}
